<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Loader;
use think\Db;
use think\Session;

/**
 *
 * @author 时 登录控制器
 *        
 */
class Admin extends Controller
{

    /*
     * 登录页面
     */
    public function index()
    {
        return view('login');
    }

    public function login()
    {
        if (Request::instance()->isPost() && ! Request::instance()->isAjax()) {
            
            $captcha = input('post.captcha');
            
            if (empty($captcha)) {
                $this->error('请填写验证码', 'admin/admin/index');
            }
            if (! captcha_check($captcha)) {
                $this->error('验证码错误', 'admin/admin/index');
            }
            ;
            //这些都可以进行异步的进行验证
            $username = input('post.username');
            // $token=input('post.__token__');
            $password = input('post.password');
            $check = input('post.check');
            $password = md5($password);
            $validate = Loader::validate('Admin');
            $res = $validate->scene('add')->check($_POST);
            if ($res === true) {
                $where = array(
                    'username' => $username
                );
                $admin_info = Db::table('zx_admin')->field(array(
                    'admin_id',
                    'username',
                    'password'
                ))
                    ->where($where)
                    ->find();
                if (! isset($admin_info)) {
                    $this->error('用户名/密码错误','admin/admin/index');
                } else {
                    $newpassword = $admin_info['password'];
                    if ($password === $newpassword) {
                        $data=array('username'=>$admin_info['username'],'admin_id'=>$admin_info['admin_id']);
                        // 这里做cookie和session操作
                        if ($check == 'on') 
                        {
                            // 初始化
                            cookie([
                                'prefix' => 'study_',
                                'expire' => 7 * 3600
                            ]);
                            // 设置
                        
                            cookie('admin_info',$data);
                        }
                        //这里进行更新登录字段
                        db('admin')->where('admin_id',$data['admin_id'])->setField('login_time',time());
                        db('admin')->where('admin_id',$data['admin_id'])->setField('last_ip',request()->ip());
                        db('admin')->where('admin_id', $data['admin_id'])->setInc('login_count');
                        session('userinfo',$data);
                       //这里使用闭包进行数组切开处理子查询进行操作
                        $list = db('method')->field('controller,node')->where('id in'.db('role_method')->field('node_id')->where("role_id in ".db('admin_role')->field('role_id')->where('admin_id',$data['admin_id'])->buildSql())->buildSql())->select();
                        //控制器名转换为大写
                        foreach ($list as $key => $val) 
                        {
                            $list[$key]['controller'] = ucfirst($val['controller']);
                        }
                        $nodelist = array();
                        $nodelist['Index'] = array('index');
                        $nodelist['Jurisdiction']=array('index');//管理员列表
                        $nodelist['Role']=array('index');//角色列表
                        $nodelist['Node']=array('index');//方法列表
                        //遍历重新拼装
                        foreach($list as $v)
                        {
                            $nodelist[$v['controller']][] = $v['node'];
                            //把修改和执行修改 添加和执行添加 拼装到一起
                            if($v['node']=="edit")
                            {
                                $nodelist[$v['controller']][]="save";
                            }
                            if($v['node']=="add")
                            {
                                $nodelist[$v['controller']][]="doadd";
                            }
                        }
                       
                        //将权限信息放置到session中
                        Session::set('admin.nodelist',$nodelist);
                        $this->success('登录成功', 'admin/Index/index');
                    } else {
                        $this->error('用户名/密码错误','admin/admin/index');
                    }
                }
            } else {
                $this->error('请重新填写');
            }
        }
    }

    public function register()
    {}
    public function loginout()
    {
       Session::clear();
       $this->redirect('admin/admin/index');
    }
    public function clear_cookie(){
        cookie('study_admin_info',NULL);
    }
    public function err(){
        return view('/error-500');
    }
}